<template>
  <el-dialog v-model="visible" title="实时反馈" width="720" :close-on-click-modal="false">
    <el-form :model="formState" :rules="formRules" ref="formRef">
      <el-form-item label="" label-position="top" prop="content">
        <el-input
          v-model="formState.content"
          style="width: 100%"
          :rows="12"
          type="textarea"
          placeholder="请输入反馈内容"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="handleSubmit()">提交</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, watchEffect } from 'vue'
import type { FormInstance } from 'element-plus'

const visible = defineModel({ type: Boolean, required: true, default: false })
const formRef = ref<FormInstance>()
const formState = reactive({
  content: ''
})

const validateContent = (_rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('反馈内容不能为空'))
  } else {
    callback()
  }
}

const formRules = reactive({
  content: [{ required: true, message: '请输入反馈内容' }, { validator: validateContent }]
})

const handleSubmit = () => {
  if (!formRef.value) {
    return
  }
  formRef.value.validate((valid) => {
    if (!valid) {
      return
    }
    console.log('=====', formState)
  })
}

watchEffect(() => {
  if (!visible.value) {
    formRef.value?.resetFields()
  }
})
</script>
